package com.scau.os.controller;

import com.scau.os.course_manager.bean.PCB;
import com.scau.os.deviceManagement.DeviceExecute;
import com.scau.os.deviceManagement.DeviceUse;
import com.scau.os.extension.EventSystem;
import com.scau.os.extension.MyDelegate;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import java.net.URL;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.LinkedList;
import java.util.Map;
import java.util.ResourceBundle;

import javafx.scene.control.Label;

public class diviceManagerController implements Initializable {

    @FXML
    Label A1 ;
   @FXML
    Label A2;

   @FXML
    Label B1;
   @FXML
    Label B2;
   @FXML
    Label B3;
   @FXML
    Label C1;
   @FXML
    Label C2;
    @FXML
    Label C3;


    public void paint(String device, PCB pcb)  {
        int status = DeviceUse.peripherals.get(device).getStatus();
        switch(device){
            case "A1":
                if (status == 127){
                    Platform.runLater(()->{
                        A1.setText("A1 is using by"+pcb.processID +"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval());
                        //System.out.println("A1 is using by"+pcb.processID +"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval()+" "+LocalDateTime.now());
                    });

                }else if (status == 0){
                    Platform.runLater(()->{
                                A1.setText("A1：空闲");
                            });
                }
                break;
            case "A2":
                if (status == 127) {
                    Platform.runLater(()->{
                        A2.setText("A2 is using by" + pcb.processID+"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval());
                    });
                }
                else if (status == 0){
                    Platform.runLater(()->{
                                A2.setText("A2：空闲");
                            });
                }

                break;
            case "B1":
                if (status == 127){
                    Platform.runLater(()->{
                        B1.setText("B1 is using by"+pcb.processID+"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval());
                    });
                } else if (status == 0){
                    Platform.runLater(()->{
                                B1.setText("B1：空闲");
                            });
                }
                break;
            case "B2":
                if (status == 127){
                    Platform.runLater(()->{
                        B2.setText("B2 is using by"+pcb.processID+"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval());
                    });

                } else if (status == 0){
                    Platform.runLater(()->{
                                B2.setText("B2：空闲");
                            });
                }
                break;
            case "B3":
                if (status == 127){
                    Platform.runLater(()->{
                        B3.setText("B3 is using by"+pcb.processID+"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval());
                    });

                }else if (status == 0){
                    Platform.runLater(()->{
                                B3.setText("B3：空闲");
                            });
                }
                break;
            case "C1":
                if (status == 127){
                    Platform.runLater(()->{
                        C1.setText("C1 is using by"+pcb.processID+"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval());
                    });

                } else if (status == 0){
                    Platform.runLater(()->{
                                C1.setText("C1：空闲");
                            });
                }
                break;
            case "C2":
                if (status == 127){
                    Platform.runLater(()->{
                        C2.setText("C2 is using by"+pcb.processID+"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval());
                    });
                } else if (status == 0){
                    Platform.runLater(()->{
                                C2.setText("C2：空闲");
                            });
                }
                break;
            case "C3":
                if (status == 127){
                    Platform.runLater(()->{

                        C3.setText("C3 is using by"+pcb.processID+"        剩余使用时间:"+DeviceUse.peripherals.get(device).getInterval());
                    });
                } else if (status == 0){
                    Platform.runLater(()->{
                                C3.setText("C3：空闲");
                            });
                }
                break;
        }
    }
    public void test(){
        DeviceUse deviceUse = new DeviceUse();
        PCB pcb1 = new PCB();
        deviceUse.allocatePeripheral("A1",10,pcb1);
        deviceUse.allocatePeripheral("C3",10,new PCB());
    }
    private void register(){
        EventSystem.instance.register(new MyDelegate("RefreshPeripheral") {
            @Override
            public void execute(Object data) {
                for (String name:DeviceUse.Process.keySet()){
                    paint(name,DeviceUse.Process.get(name));
                }
            }
        }).UnRegisterWhenClose(this);
        EventSystem.instance.register(new MyDelegate("SystemClockUpdate") {
            @Override
            public void execute(Object data) {
                //DeviceExecute.executePeripheral();
            }
        });
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        register();
        //test();
        for (String name:DeviceUse.Process.keySet()){
            paint(name,DeviceUse.Process.get(name));
        }
    }

}
